export default  {
    storageKey: 'med-history-',
    clearHistory(type) {
        localStorage.setItem(this.storageKey + type, '');
    },

    addToHistory(type, tag){
        let items = this.getHistory(type);
        let len = items.length;
        for(let i=0; i< len; i++){
            if(items[i].name == tag.name) {
                items.splice(i, 1);
                len--;
            }
        }
        items.unshift(tag)
        this.setHistory(type, items)
    },

    setHistory(type, items){
        try{
            let itemsString = JSON.stringify(items);
            localStorage.setItem(this.storageKey + type, itemsString);
        } catch(e){
            console.log(e)
        }
    },

    getHistory(type){
        let items = [];
        let itemsString = localStorage.getItem(this.storageKey + type);
        if(itemsString) {
            try{
                items = JSON.parse(itemsString);
            } catch(e){console.log(e)}
        } 
        return items;
    }
}
